package io.renren.modules.media.ffmpeg;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import org.bytedeco.javacpp.Loader;
import io.renren.common.utils.DateUtils;
import io.renren.common.utils.FileUtil;

import org.bytedeco.javacv.FrameGrabber.Exception;


public class RecordMedia {

	public static final String yyyyMMdd = "yyyyMMdd";
	public static final String Suffix = ".";
	public static final int cutterCount = 10;
	public static final String timeout = "10000000";
	public static final String FitterFormat = "mp4";
	public static final String M3u8Format = "ts";

	public static String record(String input,int recodeDuration)
			throws Exception, org.bytedeco.javacv.FrameRecorder.Exception {
		
		StringBuffer output = new StringBuffer();
		output.append("./media/").append(DateUtils.dateToStr(new Date(), yyyyMMdd)).append("/record").append("/");
		
		String outputPath = output.toString();
		FileUtil.judeDirExists(new File(outputPath));
		
		String outputFile = outputPath +System.currentTimeMillis()+ Suffix + FitterFormat;
		
		String ffmpeg = Loader.load(org.bytedeco.ffmpeg.ffmpeg.class);
		ProcessBuilder pb = new ProcessBuilder(ffmpeg, "-i", input, "-t", String.valueOf(recodeDuration), "-c", "copy",
				outputFile);
		try {
				pb.inheritIO().start().waitFor();
		} catch (IOException | InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
		
		return outputFile;
	
	}
	
	
	public static void main(String[] args) throws InterruptedException {
		try {
			record("http://223.110.245.159/ott.js.chinamobile.com/PLTV/3/224/3221225838/index.m3u8",10);
		} catch (Exception | org.bytedeco.javacv.FrameRecorder.Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}


}
